<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">


    <title>${siteName!""}--发布物品</title>
    <link rel="icon" href="/home/imgs/favicon.ico" type="image/x-icon">
    <link media="all" href="/home/css/index.css" type="text/css" rel="stylesheet">
    <link media="all" href="/home/css/release_product.css" type="text/css" rel="stylesheet">




</head>

<body>

<#include "../common/top_header.ftl">
<#include "../common/left_menu.ftl">
<div class="container">
    <div class="main center">
        <img class="release-icon-main" src="/home/imgs/release-icon.png" alt="">
        <div class="wave-fluid"></div>
        <div class="release-title">发布商品出售</div>
        <form action="publish" id="publish-form" method="post">
            <div class="form-wr">
                <div class="form-must-wr">

                    <input id="photo" type="hidden" name="photo" value="" class="required" tips="请上传图片">
                    <div id="show-img" class="form-item l goods-title" style="height: 100px;display: none;" >
                        <div class="form-key">
                            <span>已上传图片</span></div>
                        <div class="form-value">
                            <div class="form-input-wr">
                                <img id="uploaded-img" src="/home/imgs/release-icon.png" width="100px" height="100px">
                            </div>
                        </div>
                    </div>
                    <div class="form-item l goods-title">
                        <div class="form-key">
                            <span>选择商品图片</span></div>
                        <div class="form-value">
                            <div class="form-input-wr">
                                <input type="file" id="uploadFile" />
                            </div>
                        </div>
                    </div>

                    <div class="form-item l goods-title">
                        <div class="form-key">
                            <span>商品名称</span></div>
                        <div class="form-value">
                            <div class="form-input-wr">
                                <input id="name" name="name" placeholder="最多18个字" maxlength="18" value="" type="text"class="required" tips="请填写商品名称"></div>
                        </div>
                    </div>

                    <div class="form-item xl goods-desc">
                        <div class="form-key">
                            <span>商品详情</span></div>
                        <div class="form-value">
                            <div class="form-input-wr">
                                <textarea name="content" id="desc" placeholder="建议填写物品用途、新旧程度、原价等信息，至少15个字"class="required" tips="请填写详情描述"></textarea>
                            </div>
                        </div>
                    </div>


                    <div class="form-item m goods-price">
                        <div class="form-key">
                            <span>库存</span></div>
                        <div class="form-value">
                            <div class="form-input-wr">
                                <input class="price required" id="stock" name="stock" value="" type="number"  tips="请填写库存"></div>
                        </div>
                    </div>
                    <div class="form-item m goods-price">
                        <div class="form-key">
                            <span>售出价格</span></div>
                        <div class="form-value">
                            <div class="form-input-wr">
                                <input class="price required" id="sellPrice" name="sellPrice" value="" type="number"  tips="请填写出售价格"></div>
                        </div>
                    </div>



                    <!--选择分类信息 -->
                    <div class="form-item m goods-cat">
                        <div class="form-key">
                            <span>分类</span>
                        </div>
                        <div class="form-value">
                            <div class="form-input-wr">
                                <select id="cid">
                                    <option>---大类----</option>
                                    <#if goodsCategorys??>
                                    <#list goodsCategorys as goodsCategory>
                                        <#if goodsCategory.parent??>
                                            <#else >
                                    <option value="${goodsCategory.id}">${goodsCategory.name}</option>
                                        </#if>
                                    </#list>
                                    </#if>
                                </select>
                                <select id="cid2" name="goodsCategory.id">
                                    <option value="-1" >----小类----</option>
                                    <#if goodsCategorys??>
                                        <#list goodsCategorys as goodsCategory>
                                            <#if goodsCategory.parent??>
                                                <option style="display: none;" value="${goodsCategory.id}" pid="${goodsCategory.parent.id}">${goodsCategory.name}</option>
                                            </#if>
                                        </#list>
                                    </#if>
                                </select>


                            </div>
                        </div>
                    </div>



                </div>

                <input  id="submit-btn" class="form-submit" type="button" value="发布" />
            </div>
        </form>
    </div>
</div>


<#include "../common/right_menu.ftl">
<#include "../common/bottom_fotter.ftl">
<script  src="/home/js/jquery-3.1.1.min.js"></script>
<script src="/home/js/common.js"></script>
<script src="/home/js/add.js"></script>

<script type="text/javascript">
    $(document).ready(function(){
        $("#cid").change(function(){
            var cid =$(this).val();
            $("#cid2 option[value='-1']").prop("selected",true);
            $("#cid2 option[value!='-1']").css({'display':'none'});
            $("#cid2 option").each(function (i,e){
                if($(e).attr('pid')==cid){
                    $(e).css({'display':'block'})
                }
            });
            // for (var i=0;i<secondCategory.length;i++){
            //     if(cid==secondCategory[i].parent.id){
            //         str+= '<option value="'+secondCategory[i].id+'">'+ secondCategory[i].name+'</option>>';
            //     }
            // }
            // $("#cid2").children().remove();
            // $("#cid2").append(str);
        });
    });
    $("#submit-btn").click(function (){
        var flag =true;
        $(".required").each(function (i,e){
            if($(e).val()==''){
                alert($(e).attr('tips'));
                flag =false;
                return false;
            }
        });
       if (flag){
           if ($("#desc").val().length<15){
               alert('详情描述不能少于15字');
               return;
           }
           if(parseFloat($("#stock").val())=='NaN'){
               alert('库存只能输入数字');
               return;
           }

           if(parseFloat($("#sellPrice").val())=='NaN'){
               alert('价格只能输入数字');
               return;
           }
           //检查分类
           if($("#cid2").val()==''||$("#cid2").val()=='-1'){
               alert('请选择物品分类');
               return;
           }
           //准备提交表单
           ajaxRequest('publish','post',$("#publish-form").serialize(),function (){
               window.location.href="index";
           });
       }
    });
    $("#uploadFile").change(function (){
        uploadPhoto('uploaded-img','photo');
    })
    function uploadPhoto(showPictureImg,input){
        //formdata
        var formData = new FormData();
        formData.append('photo',document.getElementById('uploadFile').files[0]);
        $.ajax({
            url:'/home/upload/upload_photo',
            contentType:false,
            processData:false,
            data:formData,
            type:'POST',
            success:function(data){
                $('.loading').addClass("hide");
                if(data.code == 0){
                    $("#show-img").show();
                    // $(".pop-tip-txt").text('图片上传成功');
                    $(".pop-tip").removeClass('hide');
                    $("#"+showPictureImg).attr('src','/photo/view?filename=' + data.data);
                    // $("#"+showPictureImg).attr('old-src','/photo/view?filename=' + data.data);
                    //$("#"+input).val(data.data);
                    $("#"+input).val(data.data);
                    //向后台发送请求，修改数据库头像

                }else{
                    data = $.parseJSON(data);
                    alert(data.msg);
                }

            },
            error:function(data){
                alert('网络错误!');
            }
        });
    }
</script>
</head>

</html>
